package com.one.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.one.bean.User;
import com.one.bean.UserLogin;
import com.one.mapper.UserLoginMapper;
import com.one.mapper.UserMapper;
import com.one.service.UserService;
import com.one.vo.UserVo;
import com.one.vo.WechatUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;

/**
 * @Author ShiWen
 * @date 2022/1/14 10:39
 * @Version 1.0
 * @Description TODO 用户业务层实现类
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserLoginMapper userLoginMapper;
    @Autowired
    private UserMapper userMapper;
    /**
     *  根据手机号登录
     */
    @Override
    public UserVo loginByPhone(String phone) {
        UserLogin userLogin = this.selectUserLoginByPhone(phone);
        // 登陆成功，查询用户信息
        User user = selectUserById(userLogin.getUserId());
        // 详细信息
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(userLogin,userVo);
        BeanUtils.copyProperties(user,userVo);
        return userVo;
    }

    @Override
    public UserLogin isLoginByPwd(String phone, String password) {
        //创建查询条件
        QueryWrapper<UserLogin> queryWrapper = new QueryWrapper<>();
        // 传入手机号，和密码
        queryWrapper.eq("phone",phone).eq("password",password);
        return userLoginMapper.selectOne(queryWrapper);
    }

    @Override
    public User selectUserById(Integer userid) {
        return userMapper.selectById(userid);
    }

    @Override
    public boolean isPhone(String phone) {
        //创建查询条件
        QueryWrapper<UserLogin> queryWrapper = new QueryWrapper<>();
        // 传入手机号，和密码
        queryWrapper.eq("phone",phone);
        UserLogin userLogin = userLoginMapper.selectOne(queryWrapper);
        return userLogin!=null;
    }

    @Transactional
    @Override
    public boolean regisByPhone(String phone) {
        // 创建用户信息类，初始化信息
        User user = new User();
        // 默认昵称
        user.setNickname(phone);
        // 默认头像
        user.setHeadimg("head");
        // 注册时间和更新时间
        Date date = new Date();
        user.setCreateDate(date);
        user.setUpdateDate(date);
        //插入用户信息
        int u = userMapper.insert(user);
        // 初始化用户登录信息
        UserLogin userLogin = new UserLogin();
        userLogin.setCreateDate(date);
        userLogin.setUpdateDate(date);
        userLogin.setUserId(user.getId());
        userLogin.setPhone(phone);
        int ul = userLoginMapper.insert(userLogin);
        return u>0&&ul>0;
    }
    @Transactional
    @Override
    public boolean regisByWechatBindingPhone(JSONObject userinfo, String phone) {
        // 创建用户信息类，初始化信息
        User user = new User();
        // 默认微信昵称
        user.setNickname(userinfo.getString("nickname"));
        // 默认头像
        user.setHeadimg(userinfo.getString("headimgurl"));
        // 微信性别
        user.setGender(userinfo.getInteger("sex"));
        // 注册时间和更新时间
        Date date = new Date();
        user.setCreateDate(date);
        user.setUpdateDate(date);
        //插入用户信息
        int u = userMapper.insert(user);
        // 初始化用户登录信息
        UserLogin userLogin = new UserLogin();
        userLogin.setCreateDate(date);
        userLogin.setUpdateDate(date);
        userLogin.setUserId(user.getId());
        userLogin.setPhone(phone);
        userLogin.setOpenid(userinfo.getString("openid"));
        int ul = userLoginMapper.insert(userLogin);
        return u>0&&ul>0;
    }

    @Override
    public UserLogin selectUserLoginByPhone(String phone) {
        LambdaQueryWrapper<UserLogin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserLogin::getPhone,phone);
        return userLoginMapper.selectOne(queryWrapper);
    }

}
